package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardBattleRecordData;
import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardWinOrLoseInfo;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardRoomConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/21 17:23
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_GET_GAME_RECORD, desc = "水浒叶子牌 查看战绩")
public class ShuiHuYeZiCardGetGameRecordCmd extends AbstractCommand {

    int activityId ;
    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecordReqMsg reqMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecordReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = ShuiHuYeZiCardMgr.canEntryActivity(player,activityId);
        if(ret != 0){
            return;
        }
        ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
        List<ShuiHuYeZiCardBattleRecordData> tempList = shuiHuYeZiCardModule.getBattleRecordList(type,false);
        ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecordRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecordRespMsg.newBuilder();
        if(tempList != null){
            List<ShuiHuYeZiCardBattleRecordData> list = null;
            synchronized (tempList){
                list = new ArrayList<>(tempList);
            }
            for (ShuiHuYeZiCardBattleRecordData item : list){
                ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecord.Builder record = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecord.newBuilder();
                record.setRoomType(item.getRoomType());
                record.setIsWin(item.getIsWin());
                record.setRemainCardNum(item.getRemainCardNum());
                record.setAddRankScore(item.getAddRankScore());
                record.setCurRankScore(item.getCurRankScore());
                record.setTargetRemainCardNum(item.getTargetRemainCardNum());
                if(item.getTargetInfo() != null){
                    record.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(item.getTargetInfo()));
                }
                record.setNpcId(item.getNpcId());
                record.setTime((int)(item.getStartTime()/1000));
                resp.addRecordList(record);
            }
        }
        int winNum = 0;
        int totalNum = 0;
        int winRate = 0;
        for (Map.Entry<Integer, ShuiHuYeZiCardWinOrLoseInfo> entry: shuiHuYeZiCardModule.getUserData().getRoomWinLoseMap().entrySet()){
            int roomId = entry.getKey();
            ShuiHuYeZiCardRoomConfig roomConfig = ShuiHuYeZiCardMgr.getConfig().getRoomConfigMap().get(roomId);
            if(roomConfig.getType() == type){
                winNum += entry.getValue().getWinNum();
                totalNum += entry.getValue().getWinNum();
                totalNum += entry.getValue().getLoseNum();
            }
        }
        if(totalNum > 0){
            // 向上取整
            winRate = (winNum * 100 + totalNum - 1 )/ totalNum;
        }
        resp.setWinRate(winRate);
        resp.setWinNum(winNum);
        resp.setRet(0);
        player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_GET_GAME_RECORD,resp);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecordRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetGameRecordRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_GET_GAME_RECORD,resp);
        }
    }
}
